package com.dony.web.controller.shop;

import com.dony.common.core.controller.BaseController;
import com.dony.common.core.domain.AjaxResult;
import com.dony.common.core.page.TableDataInfo;
import com.dony.common.utils.bean.BeanUtils;
import com.dony.shop.domain.GlobalProduct;
import com.dony.shop.domain.vo.GlobalProductVo;
import com.dony.shop.domain.vo.PublishShopVo;
import com.dony.shop.service.IGlobalProductService;
import com.dony.shop.service.IPublishTaskService;
import com.dony.shop.service.ShopeeAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/shopee/globalProduct")
public class GlobalProductController extends BaseController {

    @Resource
    private ShopeeAuthService shopeeAuthService;

    @Resource
    IGlobalProductService globalProductService;

    @Resource
    IPublishTaskService publishTaskService;

    /**
     * 同步全球商品列表
     */
    @PostMapping("/sync")
    public AjaxResult globalproductSync(@RequestBody GlobalProduct globalProduct) {
        globalProductService.syncGlobalItemList(globalProduct.getMerchantId());
        return AjaxResult.success();
    }


    /**
     * 全球商品发布
     */
    @PostMapping("/publish")
    public AjaxResult publishGlobalPro(@RequestBody List<GlobalProductVo> products) {
        publishTaskService.publishBatch(products);
        return AjaxResult.success();
    }

    /**
     * 全球商品查询
     *
     * @param globalProductVo
     * @return
     */
    @RequestMapping("/list")
    public TableDataInfo getGlobalProList(GlobalProductVo globalProductVo) {
        GlobalProduct gp = new GlobalProduct();
        BeanUtils.copyProperties(globalProductVo,gp);
        startPage();
        List<GlobalProduct> listByParam = globalProductService.getListByParam(gp);
        TableDataInfo dataTable = getDataTable(listByParam);
        List<GlobalProduct> rows = (List<GlobalProduct>) dataTable.getRows();
        List<GlobalProductVo> globalProductVos = globalProductService.pakageVoList(rows, false);
        dataTable.setRows(globalProductVos);
        return dataTable;
    }

    /**
     * 全球商品已经发布的商店
     * @param merchantId
     * @param globalItemId
     * @return
     */
    @RequestMapping("/published")
    public AjaxResult getPulishedShop(@RequestParam Long merchantId , @RequestParam Long globalItemId) {
        List<PublishShopVo> list = globalProductService.getPulishedShopList(merchantId, globalItemId);
        return AjaxResult.success(list);
    }

    /**
     * 根据选择的ID 查询全球商品，并带入价格
     *
     * @param ids
     * @return
     */
    @RequestMapping("/price/{ids}")
    public AjaxResult getPriceDetail(GlobalProduct globalProduct, @PathVariable Long[] ids) {
        //暂时不封装查询条件 globalProduct
        List<GlobalProductVo> list = globalProductService.getPriceList(globalProduct, ids);
        return AjaxResult.success(list);
    }

    /**
     * 根据重量和价格计算其他站点价格
     *
     * @param
     * @return
     */
    @RequestMapping("/price/compute")
    public AjaxResult getLinePriceDetail(@RequestBody GlobalProductVo productVo) {
        return AjaxResult.success(globalProductService.getLinePriceDetail(productVo));
    }


}
